import { App, PluginSettingTab, Setting } from "obsidian";
import MyPlugin from "../../main";
import SearchModal from "../常用功能/重要功能/史锦泽的搜索框/史锦泽的搜索框";

/**
 * 添加插件命令
 * @param this_ 插件实例
 */
export function 添加命令(this_: MyPlugin): void {
	// 添加搜索文件命令 - 主要搜索功能
	this_.addCommand({
		id: 'search-files-primary',
		name: 'SJZ - 搜索页面',
		callback: () => {
			new SearchModal(this_.app,this_).open();
		}
	});

	// 添加搜索文件命令 - 备用入口
	this_.addCommand({
		id: 'search-files-shijinze',
		name: 'SJZ - 快速搜索',
		callback: () => {
			new SearchModal(this_.app,this_).open();
		}
	});
}

/**
 * 插件设置接口定义
 */
export interface MyPluginSettings {
	/** 新建文件的默认路径 */
	sjzNewFilePath: string;
	/** 指定文件路径1 */
	filePath1: string;
	/** 指定文件路径2 */
	filePath2: string;
	/** 指定文件路径3 */
	filePath3: string;
	/** 指定文件路径4 */
	filePath4: string;
	/** 指定文件路径5 */
	filePath5: string;
	/** 指定文件路径6 */
	filePath6: string;
	/** 新标签页默认文件 */
	newTabFile: string;
	/** 列表节点 */
	listNode: Node;
	lastSessionId: string ; // 默认值为空字符串;
}

/**
 * 默认设置配置
 */
export const DEFAULT_SETTINGS: MyPluginSettings = {
	sjzNewFilePath: 'default',
	filePath1: "",
	filePath2: "",
	filePath3: "",
	filePath4: "",
	filePath5: "",
	filePath6: "",
	newTabFile: "",
	listNode: document.createElement("div"),
	lastSessionId: ''  ,// 默认值为空字符串,
}

/**
 * 插件设置页面类
 */
export default class SampleSettingTab extends PluginSettingTab {
	plugin: MyPlugin;

	constructor(app: App, plugin: MyPlugin) {
		super(app, plugin);
		this.plugin = plugin;
	}

	display(): void {
		const { containerEl } = this;
		const { settings } = this.plugin;

		// 清空容器
		containerEl.empty();

		// 主标题
		containerEl.createEl('h1', {
			text: "SJZ 插件设置",
			cls: "setting-item-heading"
		});

		// 新建文件路径设置区域
		this.createNewFilePathSection(containerEl, settings);

		// 快速访问文件路径设置区域
		this.createQuickAccessSection(containerEl, settings);
	}

	/**
	 * 创建新建文件路径设置区域
	 */
	private createNewFilePathSection(containerEl: HTMLElement, settings: MyPluginSettings): void {
		containerEl.createEl('h2', {
			text: "📁 新建文件设置",
			cls: "setting-item-heading"
		});

		new Setting(containerEl)
			.setName('新建笔记默认目录')
			.setDesc('设置新创建笔记的保存目录，留空则默认保存到根目录下的"日历"文件夹中')
			.addText(text => {
				text.setPlaceholder('例如: 日常笔记/新建')
					.setValue(settings.sjzNewFilePath)
					.onChange(async (value) => {
						settings.sjzNewFilePath = value;
						await this.plugin.saveSettings();
					});
				text.inputEl.style.width = '100%';
			});
	}

	/**
	 * 创建快速访问文件设置区域
	 */
	private createQuickAccessSection(containerEl: HTMLElement, settings: MyPluginSettings): void {
		containerEl.createEl('h2', {
			text: "⚡ 快速访问文件",
			cls: "setting-item-heading"
		});

		// 创建文件路径设置项
		const filePathConfigs = [
			{ key: 'filePath1' as keyof MyPluginSettings, name: '常用文件 1', desc: '设置第一个常用文件的路径' },
			{ key: 'filePath2' as keyof MyPluginSettings, name: '常用文件 2', desc: '设置第二个常用文件的路径' },
			{ key: 'filePath3' as keyof MyPluginSettings, name: '常用文件 3', desc: '设置第三个常用文件的路径' },
			{ key: 'filePath4' as keyof MyPluginSettings, name: '常用文件 4', desc: '设置第四个常用文件的路径' },
			{ key: 'filePath5' as keyof MyPluginSettings, name: '常用文件 5', desc: '设置第五个常用文件的路径' },
			{ key: 'filePath6' as keyof MyPluginSettings, name: '常用文件 6', desc: '设置第六个常用文件的路径' }
		];

		filePathConfigs.forEach((config, index) => {
			new Setting(containerEl)
				.setName(`${config.name}`)
				.setDesc(config.desc)
				.addText(text => {
					text.setPlaceholder(`例如: 项目/重要文档${index + 1}.md`)
						.setValue(settings[config.key] as string)
						.onChange(async (value) => {
							(settings[config.key] as string) = value;
							await this.plugin.saveSettings();
						});
					text.inputEl.style.width = '100%';
				});
		});

		// 新标签页默认文件设置
		containerEl.createEl('h3', {
			text: "🆕 新标签页设置",
			cls: "setting-item-heading"
		});

		new Setting(containerEl)
			.setName('新标签页默认文件')
			.setDesc('设置打开新标签页时默认显示的文件')
			.addText(text => {
				text.setPlaceholder('例如: 模板/日记模板.md')
					.setValue(settings.newTabFile)
					.onChange(async (value) => {
						settings.newTabFile = value;
						await this.plugin.saveSettings();
					});
				text.inputEl.style.width = '100%';
			});
	}
}
